﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Portal.Cms.Model
{
    public class AjaxTab
    {
        public static string GetTab(
            string SiteId,
            string TabId,
            TabItem[] TabItems,
            int  TabIndex)
        {
            string Html = GetScript(TabId) + GetTabNoScript(SiteId, TabId, TabItems, TabIndex);
            return Html;
        }

        public static string GetScript(string TabId)
        {
            return
                 " <script language=javascript>\r\n" +
                 "      function TabChange" + TabId + "(TabId, TabIndex)\r\n" +
                 "      {\r\n" +
                 "          iTabIndex=parseInt(TabIndex);\r\n" +

                 "          document.getElementById('TabMenu'+TabId).setAttribute('class', 'section-'+(iTabIndex+1));\r\n" +
                 "          Ids='txtMax'+TabId;\r\n" +
                 "          MaxTab=parseInt(document.getElementById(Ids).value);\r\n" +
                 "          for(var iIndex=0;iIndex<MaxTab;iIndex++)\r\n" +
                 "          {\r\n" +
                 "              Ids='TabContent'+TabId+iIndex;\r\n" +
                 "              if(iIndex==TabIndex)\r\n" +
                 "              {\r\n" +
                 "                  document.getElementById(Ids).style.display='block';\r\n" +
                 "              }\r\n" +
                 "              else\r\n" +
                 "              {\r\n" +
                 "                  document.getElementById(Ids).style.display='none';\r\n" +
                 "              }\r\n" +
                 "          }\r\n" +
                 "      }\r\n" +
                 " </script>\r\n";
        }


        public static string GetTabNoScript(
            string SiteId,
            string TabId,
            TabItem[] TabItems,
            int TabIndex)
        {
            // string Javascripts = "#";
            string Html =
                 " <input type=\"hidden\" id=\"txtMax" + TabId + "\" value=\"" + TabItems.Length + "\">\r\n" +
                 " <div class=tabs>\r\n" +
                 " <div id=\"TabMenu" + TabId + "\" class=\"section-" + (TabIndex + 1) + "\">\r\n" +
                 " <ul class=\"menu\">\r\n";
            for (int iIndex = 0; iIndex < TabItems.Length; iIndex++)
            {
                string JavascriptPage = "javascript:TabChange" + TabId + "('" + TabId + "','" + iIndex + "');";
                Html +=
                 "      <li id=\"nav-" + (iIndex + 1) + "\"><a href=\"" + JavascriptPage + "\">" + TabItems[iIndex].Title + "</a></li>\r\n";
            }
            Html +=
                 " </ul>\r\n" +
                 " <div id=\"contents\">\r\n";
            for (int iIndex = 0; iIndex < TabItems.Length; iIndex++)
            {
                if (iIndex == TabIndex)
                {
                    Html += "<div style=\"display:block;\" id=\"TabContent" + TabId + iIndex + "\">" + TabItems[iIndex].Body + "</div>\r\n";
                }
                else
                {
                    Html += "<div style=\"display:none;\" id=\"TabContent" + TabId + iIndex + "\">" + TabItems[iIndex].Body + "</div>\r\n";
                }
            }
            Html +=
                " </div>\r\n" +
                "</div>\r\n" +
                "</div>\r\n";

            return Html;
        }
    }
}
